# ------------------
# Techniques de développement
# M1 ALMA 2011-2012
# Pauline Folz - Adrien Quillet
# 
#
# ------------------
# Documentation
# ------------------
#
# CHECK_BOOST_PYTHON
# ------------------

AC_DEFUN([CHECK_BOOST_PYTHON],
[

AC_REQUIRE([CHECK_PYTHON])
AC_MSG_CHECKING([for Boost.Python])
AC_MSG_RESULT([])

AC_ARG_WITH([boost-include-dir], 
			[AS_HELP_STRING([--with-boost-include-dir],
							[Boost directory, like /usr/local/include/boost])],
			[boost_include_dir=$with_boost_include_dir],
			[boost_include_dir=""])
			
AC_ARG_WITH([boost-lib-dir], 
			[AS_HELP_STRING([--with-boost-lib-dir],
							[Boost directory, like /usr/local/lib/boost])],
			[boost_lib_dir=$with_boost_lib_dir],
			[boost_lib_dir=""])
			
AC_ARG_WITH([boost-tmpdir-name], 
			[AS_HELP_STRING([--with-boost-tmpdir-name],
							[Where to work ; default = tmp_boost_test])],
			[boost_tmpdir_name=$with_boost_tmpdir_name],
			[boost_tmpdir_name="tmp_boost_test"])
			
CXXFLAGS_save="$CXXFLAGS"
LDFLAGS_save="$LDFLAGS"

if ! test "$boost_include_dir" = "" ; then
	boost_include_dir=`echo $boost_include_dir | sed 's/:/ -I/g'`
	CXXFLAGS="$CXXFLAGS -I$boost_include_dir"
fi

if ! test "$boost_lib_dir" = "" ; then
	LDFLAGS="$LDFLAGS -L$boost_lib_dir"
fi

LDFLAGS="$LDFLAGS $PYTHON_LIB -lboost_python"
CXXFLAGS="$CXXFLAGS -I$PYTHON_INCLUDE_DIR"

# On créer un repertoire temporaire pour travailler.
AC_MSG_NOTICE([Creating $boost_tmpdir_name])

if ! test -d "$boost_tmpdir_name" ; then
	mkdir "$boost_tmpdir_name"
	if ! test -w "$boost_tmpdir_name"  ; then
		AC_MSG_ERROR([can not write into directory $boost_tmpdir_name])
	fi
fi

AC_MSG_NOTICE([Moving into $boost_tmpdir_name])
cd "$boost_tmpdir_name"

AC_MSG_NOTICE([Creating test_boost_python.cpp])
cat > test_boost_python.cpp << "EOF"
#include <boost/python.hpp>

char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

int main()
{ 
	return 0 ;
}
EOF

AC_MSG_CHECKING([if we can build test_boost_python.cpp])
AC_TRY_EVAL("$CXX $CXXFLAGS -o test_boost_python test_boost_python.cpp $LDFLAGS")
if ! test "$ac_status" = 0 ; then
	AC_MSG_RESULT([no])
	HAVE_BOOST_PYTHON=no
else
	HAVE_BOOST_PYTHON=yes
	AC_MSG_RESULT([yes])
fi

AC_MSG_NOTICE([Moving into ..])
cd ..

AC_MSG_NOTICE([Deleting boost_tmpdir_name])
rm -rf "$boost_tmpdir_name"

CXXFLAGS="$CXXFLAGS_save"
LDFLAGS="$LDFLAGS_save"

if test "$HAVE_BOOST_PYTHON" = yes ; then
	BOOST_PYTHON_INCLUDE_DIR="$boost_include_dir"
	BOOST_PYTHON_LIB_DIR="$boost_lib_dir"
	BOOST_PYTHON_LIB="-lboost_python $PYTHON_LIB"
	
	if test "$BOOST_PYTHON_LIB_DIR" = "" ; then
		split_path=`echo $PATH | sed 's/:/ /g'`
		split_path="$split_path /usr/include /usr/local/include"
		for tmp in $split_path ; do
			if test -f "$tmp/libboost_python.so" ; then
				BOOST_PYTHON_LIB_DIR="$tmp"
				break  ;
			fi
		done
	fi
	
	if test "$BOOST_PYTHON_INCLUDE_DIR" = "" ; then
		split_path=`echo $PATH | sed 's/:/ /g'`
		split_path="$split_path /usr/include /usr/local/include"
		for tmp in $split_path ; do
			if test -f "$tmp/boost/python.hpp" ; then
				BOOST_PYTHON_INCLUDE_DIR="$tmp"
				break  ;
			fi
		done
	fi
	
	if test "$BOOST_PYTHON_LIB_DIR" = "" || test "$BOOST_PYTHON_INCLUDE_DIR" = "" ; then
		AC_MSG_WARN([Unable to find path to Boost.Python.
					 Please use --with-boost-include-dir and --with-boost-lib-dir])
	fi
	
	AC_SUBST([BOOST_PYTHON_INCLUDE_DIR])
	AC_SUBST([BOOST_PYTHON_LIB_DIR])
	AC_SUBST([BOOST_PYTHON_LIB])
fi

AC_MSG_RESULT([results of Boost.Python check :])
AC_MSG_RESULT([	Have Boost.Python : $HAVE_BOOST_PYTHON])
if test "$HAVE_BOOST_PYTHON" = yes ; then
	AC_MSG_RESULT([	Include dir       : $BOOST_PYTHON_INCLUDE_DIR])
	AC_MSG_RESULT([	Lib dir           : $BOOST_PYTHON_LIB_DIR])
	AC_MSG_RESULT([	Libs              : $BOOST_PYTHON_LIB])
fi

])
